package com.baizhi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.http.StreamingHttpOutputMessage;
import tk.mybatis.mapper.common.Mapper;

import java.util.Random;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>请参见最新版本号</version>
</dependency>
*/
public class SendSmsUtil {
    //发送验证码
    public static String sendPhoneCode(String phoneNumber,String randomCode){
        //修改公钥和私钥
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5t6d91zGvvtkTr3gD7wY", "x1dDAQOVSE3WE8g91tgpD1qazuTnIb");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);

        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //修改要接收验证码的手机号码
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        //配置以审核通过的签名
        request.putQueryParameter("SignName", "小蛋黄");
        //配置以审核通过的模板code
        request.putQueryParameter("TemplateCode", "SMS_171112532");
        //配置code变量的值
        request.putQueryParameter("TemplateParam", "{\"code\":\""+randomCode+"\"}");

        CommonResponse response = null;

        try {
            response = client.getCommonResponse(request);
            //System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response.getData();
    }

    public static String sendPhone(String phoneNumber,String randomCode){
        String message = sendPhoneCode(phoneNumber, randomCode);
        JSONObject jsonObject = JSON.parseObject(message);
        Object code = jsonObject.get("Code");

        String codeMsg = null;
        if(code.equals("OK")) codeMsg="发送成功";
        if(code.equals("isv.OUT_OF_SERVICE")) codeMsg="业务停机";
        if(code.equals("isv.ACCOUNT_NOT_EXISTS")) codeMsg="账户不存在";
        if(code.equals("isv.ACCOUNT_ABNORMAL")) codeMsg="账户异常";
        if(code.equals("isv.MOBILE_NUMBER_ILLEGAL")) codeMsg="非法手机号";
        return codeMsg;
    }

    //生成随机数
    public static  String  getRandom(int n){
        char[] code =  "0123456789".toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(code[new Random().nextInt(code.length)]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        //接收前台传递的手机号
        String phone = "18834877084";
        //获取随机数
        String random = getRandom(6);
        //将随机数存储redis
        System.out.println(random);
        //发送验证码
        String msg = sendPhoneCode(phone, random);
        //Object parse = JSON.parse(msg);
        JSONObject jsonObject = JSON.parseObject(msg);
        String code = (String) jsonObject.get("Code");
        System.out.println(code);
    }
}
